package com.bhs.sansong.ui.findLogistics;

import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.EvaluationAdapter;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.EvaluationBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 公司评价列表
 */
public class CompanyEvaluationListFragment extends BaseFragment implements NetCallBack {


    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private EvaluationAdapter mAdapter;
    private int page = 1;
    private int logistics_id;
    private String type;

    public static CompanyEvaluationListFragment newInstance(String type, int logistics_id) {
        CompanyEvaluationListFragment fragment = new CompanyEvaluationListFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        args.putInt("logistics_id", logistics_id);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int initLayout() {
        return R.layout.fragment_company_evaluation_list;
    }

    @Override
    public void initView(View root) {
        logistics_id = getArguments().getInt("logistics_id", 0);
        if (logistics_id == 0) {
            logistics_id = SharedPreferencesUtils.getInt(getContext(), "logistics_id",0);
        }
        type = getArguments().getString("type");
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        mRvList = root.findViewById(R.id.rv_list);
        mAdapter = new EvaluationAdapter();
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));

    }

    @Override
    public void initData() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
        loadData();
    }

    Map<String, Object> map = new ArrayMap<>();

    private void loadData() {
        map.put("platform", "cargo");
        map.put("page", page);
        map.put("limit", 10);
        map.put("logistics_id", logistics_id);
        map.put("type", type);
        networkRequest(netApi.get_comment_list(map), this);
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        EvaluationBean bean=new Gson().fromJson(json,EvaluationBean.class);
        mRefreshLayout.setRefreshing(false);
        if (bean.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}